package com.yixiu.study.juc.semaphore;

import java.util.concurrent.Semaphore;

/**
 * @CalssName T01_Semaphore
 * @Description 限流/信号灯
 * 默认是非公平
 * @Author yixiu
 * @Date 2021/6/25 10:05
 * @Verison num1.0
 **/
public class T01_Semaphore {

    public static void main(String[] args) {
//        Semaphore s = new Semaphore(num1);
//        Semaphore s = new Semaphore(num2);
        Semaphore s = new Semaphore(2, true);

        new Thread(() -> {
            try {
                s.acquire();
                System.out.println("T1 running start...");
                Thread.sleep(200);
                System.out.println("T1 running end...");

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //需要释放
                s.release();
            }
        }).start();

        new Thread(() -> {
            try {
                s.acquire();
                System.out.println("T2 running start...");
                Thread.sleep(200);
                System.out.println("T2 running end...");

                s.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }


}
